﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_ORDER.BizLogic;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_ORDER.OrderObject.UserControls;
using ERP_RORZE_PERMISSION.Base;
using MakerHelper = ERP_RORZE_ORDER.BizLogic.MakerHelper;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_ORDER.OrderObject.Forms
{
    public partial class frm_SupplierEdit : DevExpress.XtraEditors.XtraForm
    {
        private ucSupplier parent;

        public string SupplierID;
        public string SupplierName;
        private LogUtils log;
        public string Note;

        public frm_SupplierEdit(ucSupplier ucP)
        {
            InitializeComponent();
            parent = ucP;
            log = new LogUtils();
            this.CancelButton = this.btCancel;
            this.ActiveControl = txtSupplierID;
        }

        private void frm_ProductAdd_Load(object sender, EventArgs e)
        {
            BindData();
        }


        private void btEdit_Click(object sender, EventArgs e)
        {
            SupplierHelper helper = new SupplierHelper(new Supplier());
            if (GetData(helper.ValueObject))
            {

                if (helper.Update() > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.F2Order.Supplier;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "SupplierID: " + helper.ValueObject.TempId + "; SupplierName: " + helper.ValueObject.SupplierName;
                    log.WriteLog();

                    if (Utilities.MessageConfirmationYesNo(OrderConstants.Message.UpdateSuccessAndQuestion))
                    {

                        txtSupplierID.Focus();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(OrderConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        #region Private Method
        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private bool GetData(Supplier o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(txtSupplierID.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Supplier.SupplierIDCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtSupplierName.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Supplier.SupplierNameCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            //if (CheckDuplicate(txtProductID.Text.Trim()))
            //{
            //    MessageBox.Show(OrderConstants.Message.Product.ProductHasExisted, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            #endregion

            o.TempId = txtSupplierID.Text;
            o.SupplierID = SupplierID;
            o.SupplierName = txtSupplierName.Text;
            o.Note = txtNote.Text;

            return true;
        }

        private void BindData()
        {
            txtSupplierID.Text = SupplierID;
            txtSupplierName.Text = SupplierName;
            txtNote.Text = Note;
        }

        #endregion

        private void frm_SupplierEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btEdit.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btEdit.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                txtSupplierID.Focus();
            }
        }


    }
}